from sqlalchemy.orm import Session

# from src.db.models import User, UserCreate, UserUpdate
from app.models.models import DUser, DAccount, DIconFile
from app.schemas.schemas import *
from app.security.security import get_password_hash
from typing import List, Optional

################ user - read #################


def get_user(db: Session, user_id: int):
    return db.query(DUser).filter(DUser.id == user_id).first()


def get_all_users(db: Session):
    return db.query(DUser).all()


def get_user_by_id(db: Session, user_id: int):
    return db.query(DUser).filter(DUser.id == user_id).first()


def get_user_by_username(db: Session, username: str):
    return db.query(DUser).filter(DUser.username == username).first()


################ user - create #################


def create_user(db: Session, user: UserCreate):
    db_user = DUser(**user.model_dump())
    db.add(db_user)
    db.commit()
    db.refresh(db_user)
    return db_user


################ user - update #################


def update_user(db: Session, user: User):
    db_user = db.query(DUser).filter(DUser.id == user.id).first()
    if db_user:
        db_user.username = user.username
        db_user.hashed_password = user.hashed_password
        db_user.email = user.email
        db.commit()
        db.refresh(db_user)
    return db_user


def update_user_password(db: Session, user_id: int, new_password: str):
    db_user = db.query(DUser).filter(DUser.id == user_id).first()
    if db_user:
        db_user.hashed_password = get_password_hash(new_password)
        db.commit()
        db.refresh(db_user)
        return db_user
    return None


################ user - delete #################


def delete_user(db: Session, user_id: int):
    db_user = db.query(DUser).filter(DUser.id == user_id).first()
    if db_user:
        db.delete(db_user)
        db.commit()
        return True
    return False


###################### Account #########################


def get_accounts(db: Session, user_id: int) -> List[DAccount]:
    accounts = db.query(DAccount).filter(DAccount.owner_id == user_id).all()

    # return [AccountInfo.model_validate(account) for account in accounts]
    return accounts


def create_account(
    db: Session, account: AccountCreate, owner_id: int
) -> Optional[DAccount]:
    """创建用户账户"""
    db_account = DAccount(
        owner_id=owner_id,
        account_username=account.account_username,
        account_password=account.account_password,
        account_email=account.account_email,
        description=account.description,
        icon_id=account.icon_id,
        tag=account.tag,
        remark=account.remark,
    )
    db.add(db_account)
    db.commit()
    db.refresh(db_account)
    return db_account


def update_account(
    db: Session, account: AccountUpdate, owner_id: int, account_id: int
) -> Optional[DAccount]:
    db_account = (
        db.query(DAccount)
        .filter(DAccount.id == account_id, DAccount.owner_id == owner_id)
        .first()
    )

    if not db_account:
        return None

    # 获取非None的更新字段
    update_data = account.model_dump(exclude_unset=True)

    for filed, value in update_data.items():
        setattr(db_account, filed, value)

    db.commit()
    db.refresh(db_account)

    return db_account


def delete_account(db: Session, owner_id: int, account_id: int):
    db_account = (
        db.query(DAccount)
        .filter(DAccount.id == account_id, DAccount.owner_id == owner_id)
        .first()
    )
    if db_account:
        db.delete(db_account)
        db.commit()
        return True
    return False

def update_icon_file(
    db: Session, icon_file_id: int, icon_file: IconFileUpdate
) -> Optional[DIconFile]:
    """更新图标文件"""
    db_icon_file = db.query(DIconFile).filter(DIconFile.id == icon_file_id).first()
    if not db_icon_file:
        return None

    # 获取非None的更新字段
    update_data = icon_file.model_dump(exclude_unset=True)

    for field, value in update_data.items():
        setattr(db_icon_file, field, value)

    db.commit()
    db.refresh(db_icon_file)
    return db_icon_file

# # 获取用户的所有账户
# user = db.query(DUser).filter(DUser.id == 1).first()
# user_accounts = user.accounts.all()  # 返回该用户的所有账户列表

# # 新增账户
# new_account = Account(name="工资账户", owner_id=user.id)
# db.add(new_account)
# db.commit()

def create_icon_file(
    db: Session, icon_file: IconFileCreate, owner_id: int
) -> Optional[DIconFile]:
    """创建图标文件"""
    db_icon_file = DIconFile(
        file_name=icon_file.file_name,
        file_url=icon_file.file_url,
        file_size=icon_file.file_size,
        file_type=icon_file.file_type,
    )
    db.add(db_icon_file)
    db.commit()
    db.refresh(db_icon_file)
    return db_icon_file


def get_icon_file(db: Session, icon_file_id: int) -> Optional[DIconFile]:
    """根据ID获取图标文件"""
    return db.query(DIconFile).filter(DIconFile.id == icon_file_id).first()

def get_icon_files(db: Session) -> List[DIconFile]:
    """获取用户的所有图标文件"""
    return db.query(DIconFile).all()

def delete_icon_file(
    db: Session, icon_file_id: int
) -> bool:
    """删除图标文件"""
    db_icon_file = (
        db.query(DIconFile)
        .filter(DIconFile.id == icon_file_id)
        .first()
    )
    if db_icon_file:
        db.delete(db_icon_file)
        db.commit()
        return True
    return False